****APPENDIX FIGURE A1 and A2****

cd "$main_dir/output/figures/appendix_a"

graph set window fontface "Latin Modern Math"

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

use "$data_dir/clean/ivs_clean_collapsed.dta", clear
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_ivs
}

merge 1:1 ccode using "$data_dir/clean/gallup_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_gallup
}

merge 1:1 ccode using "$data_dir/clean/wjp_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_wjp
}

merge 1:1 ccode using "$data_dir/clean/afro_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_afro
}

merge 1:1 ccode using "$data_dir/clean/latino_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_latino
}

merge 1:1 ccode using "$data_dir/clean/americasbarometer_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_americas
}

merge 1:1 ccode using "$data_dir/clean/arab_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_arab
}

merge 1:1 ccode using "$data_dir/clean/lits_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_lits
}

merge 1:1 ccode using "$data_dir/clean/asia_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_asia
}

merge 1:1 ccode using "$data_dir/clean/southasia_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_southasia
}

merge 1:1 ccode using "$data_dir/clean/ess_clean_collapsed.dta", nogen keepusing(trust_others trust_state)
local varlist trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_ess
}

count
gen n = _n/170

//--------------------------------------------------------
//Create Figure A1
//--------------------------------------------------------

//Gallup and IVS
pwcorr trust_others_gallup trust_others_ivs, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_ivs, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(0(0.2)1,labs(small)) xlabel(0(0.2)1,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (IVS)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_ivs.gph", replace

//Gallup and WJP
pwcorr trust_others_gallup trust_others_wjp, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_wjp, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (WJP)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_wjp.gph", replace

//Gallup and Latinobarometer/Americasbarometer
cap drop trust_others_lat_am
gen trust_others_lat_am = trust_others_latino
replace trust_others_lat_am = trust_others_americas if trust_others_lat_am==.

pwcorr trust_others_gallup trust_others_lat_am, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

pwcorr trust_others_gallup trust_others_latino, sig star(.01)
scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_latino, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others" "(Latinobarometer/Americasbarometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_latino.gph", replace

//Gallup and Arab Barometer
pwcorr trust_others_gallup trust_others_arab, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_arab, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (Arab Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_arab.gph", replace

//Gallup and LITS
pwcorr trust_others_gallup trust_others_lits, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_lits, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (Life in Transition Survey)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_lits.gph", replace

//Gallup and Asia Barometer

pwcorr trust_others_gallup trust_others_asia, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_asia, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (Asia Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_asia.gph", replace

//Gallup and Afrobarometer

pwcorr trust_others_gallup trust_others_afro, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_afro, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (Afrobarometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_afro.gph", replace

//Gallup and ESS

pwcorr trust_others_gallup trust_others_ess, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_others_gallup trust_others_ess, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Others (Gallup)", size(small)) xtitle("Trust in Others (ESS)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_others_gallup_ess.gph", replace

//combine graphs
gr combine trust_others_gallup_ivs.gph trust_others_gallup_wjp.gph trust_others_gallup_latino.gph trust_others_gallup_arab.gph trust_others_gallup_lits.gph trust_others_gallup_asia.gph trust_others_gallup_afro.gph trust_others_gallup_ess.gph, graphregion(color(white)) note("Note: each plot includes the correlation coefficient, {&rho}, across surveys with corresponding statistical significance. Dashed line is the 45º line. The Gallup World Poll is the base survey for comparisons. Country" "averages are pooled over all survey years available between 2009-2019 where data is non-missing per survey. Data for the South Asia barometer is missing.", size(tiny))
graph export "figure_a1.png", replace

erase "trust_others_gallup_ivs.gph"
erase "trust_others_gallup_wjp.gph" 
erase "trust_others_gallup_latino.gph" 
erase "trust_others_gallup_arab.gph"
erase "trust_others_gallup_lits.gph" 
erase "trust_others_gallup_asia.gph" 
erase "trust_others_gallup_afro.gph" 
erase "trust_others_gallup_ess.gph"

//--------------------------------------------------------
//Create Figure A2
//--------------------------------------------------------

//Gallup and IVS
pwcorr trust_state_gallup trust_state_ivs, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_ivs, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(0(0.2)1,labs(small)) xlabel(0(0.2)1,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(IVS)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_ivs.gph", replace

//Gallup and WJP
pwcorr trust_state_gallup trust_state_wjp, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_wjp, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(WJP)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_wjp.gph", replace

//Gallup and Latinobarometer/Americasbarometer
cap drop trust_state_lat_am
gen trust_state_lat_am = trust_state_latino
replace trust_state_lat_am = trust_state_americas if trust_state_lat_am==.


pwcorr trust_state_gallup trust_state_lat_am, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

pwcorr trust_state_gallup trust_state_lat_am, sig star(.01)
scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_latino, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(Latinobarometer/Americasbarometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_latino.gph", replace

//Gallup and Arab Barometer
pwcorr trust_state_gallup trust_state_arab, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_arab, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(Arab Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_arab.gph", replace

//Gallup and LITS
pwcorr trust_state_gallup trust_state_lits, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_lits, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(Life in Transition Survey)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_lits.gph", replace

//Gallup and Asia/South Asia Barometer

cap drop trust_state_as_sas
gen trust_state_as_sas = trust_state_asia
replace trust_state_as_sas = trust_state_southasia if trust_state_as_sas==.

pwcorr trust_state_gallup trust_state_as_sas, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_as_sas, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(Asia/South Asia Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_asia_sasia.gph", replace

//Gallup and Afrobarometer

pwcorr trust_state_gallup trust_state_afro, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_afro, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(Afrobarometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_afro.gph", replace

//Gallup and ESS

pwcorr trust_state_gallup trust_state_ess, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_state_gallup trust_state_ess, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust State Institutions" "(Gallup)", size(small)) xtitle("Trust State Institutions" "(ESS)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_state_gallup_ess.gph", replace

//combine graphs
gr combine trust_state_gallup_ivs.gph trust_state_gallup_wjp.gph trust_state_gallup_latino.gph trust_state_gallup_arab.gph trust_state_gallup_lits.gph trust_state_gallup_asia_sasia.gph trust_state_gallup_afro.gph trust_state_gallup_ess.gph, graphregion(color(white)) note("Note: each plot includes the correlation coefficient, {&rho}, across surveys with corresponding statistical significance. Dashed line is the 45º line. The Gallup World Poll is the base survey for comparisons. Country" "averages are pooled over all survey years available between 2009-2019 where data is non-missing per survey.", size(tiny))
graph export "figure_a2.png", replace

erase "trust_state_gallup_ivs.gph"
erase "trust_state_gallup_wjp.gph" 
erase "trust_state_gallup_latino.gph" 
erase "trust_state_gallup_arab.gph" 
erase "trust_state_gallup_lits.gph" 
erase "trust_state_gallup_asia_sasia.gph" 
erase "trust_state_gallup_afro.gph" 
erase "trust_state_gallup_ess.gph"
